/**
 *
 * FileName: ${bean_type}.java
 *
 * Description: ${bean_sql}
 *
 * Created: ${bean_author} ${bean_created}
 *
 */

/**
 * ${bean_type}
 * 
 * Create From SQL: ${bean_sql}
 * 
 * @author ${bean_author} ${bean_created}
 * @version 0.0.1
 */
 
package ${controller_pkg};

import java.util.Arrays;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ${common_pkg}.base.BaseController;
import ${search_pkg}.${page_query};
import ${bean_pkg}.${bean_type};
import ${service_pkg}.${bean_type}Service;

@Controller
@RequestMapping("/${bean_type?uncap_first}")
public class ${bean_type}Controller extends BaseController<${bean_type}, ${bean_type}Search>{
	
	@Inject
	private ${bean_type}Service ${bean_type?uncap_first}Service;
	
	@RequestMapping("/${bean_type?uncap_first}List")
	public String list(){
		
		return "${bean_type?lower_case}/${bean_type?lower_case}list";
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public String editPage(Model model, Long id){
		if(id!=null){
			${bean_type} ${bean_type?uncap_first} = ${bean_type}Service ${bean_type?uncap_first}Service.queryById(id);
			model.addAttribute("${bean_type?uncap_first}", ${bean_type?uncap_first});
		}
		return String.format(EDIT, "${bean_type?uncap_first}","${bean_type?uncap_first}");
	}
	

	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public String edit(${bean_type} ${bean_type?uncap_first}){
		if(${bean_type?uncap_first}.getId()==null){
			${bean_type?uncap_first}Service.save(${bean_type?uncap_first});
		}else{
			${bean_type?uncap_first}Service.update(${bean_type?uncap_first});
		}
		
		return "redirect:/${bean_type?uncap_first}/${bean_type?uncap_first}List";
	}
	
	/**
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String save(${bean_type} ${bean_type?uncap_first}){
		${bean_type?uncap_first}Service.save(${bean_type?uncap_first});
		return "redirect:/${bean_type?uncap_first}/${bean_type?uncap_first}List";
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(${bean_type} ${bean_type?uncap_first}){
		${bean_type?uncap_first}Service.update(${bean_type?uncap_first});
		return "redirect:/${bean_type?uncap_first}/${bean_type?uncap_first}List";
	}
	*/
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String delete${bean_type}s(String ids) {
		${bean_type?uncap_first}Service.deleteByIds(Arrays.asList(ids.split(",")));
		return "redirect:/${bean_type?uncap_first}/${bean_type?uncap_first}List";
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") Long id) {
		${bean_type?uncap_first}Service.deleteById(id);
		return "redirect:/${bean_type?uncap_first}/${bean_type?uncap_first}List";
	}
}

